package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.bean.Student;
import com.itheima.mapper.StudentMapper;
import com.itheima.service.StudentService;
import com.itheima.utils.MybatisSqlSession;

import java.util.List;

/*
    学生业务层实现类
 */
public class StudentServiceImpl implements StudentService {
    /*
        删除数据方法
     */
    @Override
    public void deleteStu(String number) {
        StudentMapper mapper = (StudentMapper) MybatisSqlSession.getMapper(StudentMapper.class);
        //5.调用实现类对象删除方法
        mapper.deleteStu(number);
    }

    /*
        修改数据方法
     */
    @Override
    public void updateStu(Student stu) {
        StudentMapper mapper = (StudentMapper) MybatisSqlSession.getMapper(StudentMapper.class);
        //5.调用实现类对象修改方法
        mapper.updateStu(stu);

    }

    /*
        添加数据方法
     */
    @Override
    public void addStu(Student stu) {
        StudentMapper mapper = (StudentMapper) MybatisSqlSession.getMapper(StudentMapper.class);
        //5.调用实现类对象添加方法
        mapper.addStu(stu);

    }

    /*
        分页查询功能
     */
    @Override
    public PageInfo selectByPage(Integer currentPage, Integer pageSize) {
        StudentMapper mapper = (StudentMapper) MybatisSqlSession.getMapper(StudentMapper.class);
        // 设置分页参数
        PageHelper.startPage(currentPage, pageSize);
        // 执行查询
        List<Student> students = mapper.selectAll();
        // 封装到PageInfo对象, 并返回
        PageInfo<Student> info = new PageInfo<>(students);
        return info;
    }

    @Override
    public Page selectByPagePage(Integer currentPage, Integer pageSize) {
        StudentMapper mapper = (StudentMapper) MybatisSqlSession.getMapper(StudentMapper.class);
        // 设置分页参数
        Page page = PageHelper.startPage(currentPage, pageSize);
        // 执行查询
        List<Student> students = mapper.selectAll();
        // 封装到PageInfo对象, 并返回
        return page;
    }
}
